#include "main.h"
__HufTree ** paste (__HufTree ** H_T, __HufTree * elm_new)
{
	int i = 2;
	int n = 0;
	int m = 2;
	int k = 0;
	int tmp = 2;
	int count = 0;
	__HufTree ** huffTree_new = NULL;
	__HufTree * elm;
	while (H_T[i])	i++;
	huffTree_new = (__HufTree **) calloc (i,sizeof(__HufTree*));
	if (!huffTree_new)	cl_exit (PASTE, MEMORY_ERR);
	n = i - 1;
	i = 0;
	while (i < n)	{
		m = 2;
		elm = H_T[2];
		while (m <= n)	{
			if (H_T[m])
				if (elm)
					if (elm->count > H_T[m]->count)	{	
						elm = H_T[m];
						tmp = m;
					} else {}
				else {	
					elm = H_T[m];
					tmp = m;
				}
			if (elm_new)
				if (elm)
					if (elm->count > elm_new->count)	{
						elm = elm_new;
						tmp = -1;	
					} else {}
				else {
					elm = elm_new;
					tmp = -1;
				}
				m++;
		}
		if (elm)	{	
			huffTree_new[count] = elm;
			elm = NULL;
			if (tmp < 0)	{
				elm_new = NULL;
				while (k<n)
				{
					if (H_T [k])	{
						tmp = k;
						break;
					}
					k++;
				}
			}
			else 	H_T[tmp] = NULL;
			count ++;
		}
		i++;
	}
	return huffTree_new;
}